package com.hzw.code.fast.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzw.code.fast.sys.controller.args.SysUserPageArgs;
import com.hzw.code.fast.sys.dto.SysUserDto;
import com.hzw.code.fast.sys.dto.page.SysUserPageDto;
import com.hzw.code.fast.sys.entity.SysUserEntity;
import com.hzw.code.fast.sys.mapper.SysUserMapper;
import com.hzw.code.security.model.CustomUserDetails;
import com.hzw.code.fast.sys.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户业务层实现类
 */
@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {

    private final SysUserMapper userMapper;
    public SysUserServiceImpl(SysUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public PageInfo<SysUserDto> findSysUserList(SysUserPageArgs query) {
        // 启用分页
        PageHelper.startPage(query.getCurrentPage().intValue(),query.getSize().intValue());
        PageInfo<SysUserDto> pageInfo = new PageInfo<>(userMapper.findSysUserList(query));
        return pageInfo;
    }

    @Override
    public int getCountByName(String userName) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        return super.count(queryWrapper);
    }

    @Override
    public int getCountByPhone(String phone) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",phone);
        return super.count(queryWrapper);
    }

    @Override
    public CustomUserDetails getUserByUsername(String userAccount) {
        return userMapper.getUserByUsername(userAccount);
    }
}
